package cn.wolfcode.web.controller;


import cn.wolfcode.domain.*;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;


    @RequirePermission(name = "结算页面",expression = "consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "结算删除",expression = "consumption:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            consumptionService.delete(id);
        }
        return new JsonResult();
    }


    //无预约的新增编辑
    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "结算新增/编辑",expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consumption){
        if (consumption.getId() != null) {
            consumptionService.update(consumption);
        }else {
            consumptionService.save(consumption);
        }
        return new JsonResult();
    }

    //有预约的新增编辑,因为需要联动预约单号以及修改预约单状态,独立开来
    //需要接收预约单的流水号,根据这个流水号再生成结算单
    //即点击确认到店的后端处理
    @RequestMapping("/save")
    @RequirePermission(name = "结算新增/编辑",expression = "consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult save(String appointmentAno){
        Consumption consumption = consumptionService.save(appointmentAno);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(consumption.getId());
        return jsonResult;
    }

    //有员工自己手动新增的和每个结算单的编辑(详情)
    @RequirePermission(name = "结算新增/编辑",expression = "consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);
            //根据结算单的流水号拿到所有结算明细
            String cno = consumption.getCno();
            List<ConsumptionItem> items = consumptionItemService.selectByCno(cno);
            model.addAttribute("items",items);

        }
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses",businesses);

        return "consumption/input";
    }
}
